package com.bjfu.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjfu.common_utils.R;
import com.bjfu.common_utils.RandomUtil;
import com.bjfu.servicebase.exceptionhandler.GuliException;
import com.bjfu.staservice.client.UcenterClient;
import com.bjfu.staservice.entity.StatisticsDaily;
import com.bjfu.staservice.mapper.StatisticsDailyMapper;
import com.bjfu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Guli
 * @since 2020-07-24
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void countRegister(String day) {
        // 为了防止统计重复,先删除原先记录在进行添加
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        // 远程调用计算注册人数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(countRegister);
        statisticsDaily.setDateCalculated(day);
        // 暂时先随机生成这些值
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        // 把获取数据添加到数据库,统计分析表
        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.orderByAsc("date_calculated");
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);
        // 创建日期的list集合 创建数量的list集合
        List<String> date_calculated = new ArrayList<>();
        List<Integer> dataList = new ArrayList<>();
        String title = null;
        for (StatisticsDaily daily : staList) {
            date_calculated.add(daily.getDateCalculated());
            switch (type){
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    title = "登录人数统计";
                    break;
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    title = "注册人数统计";
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    title = "视频播放人数统计";
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    title = "课程数量统计";
                    break;
                default:
                    throw new GuliException(20001,"统计类型错误");
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("xAxisData",date_calculated);
        map.put("yAxisData",dataList);
        map.put("title",title);
        return map;
    }
}
